<extend name="Public/publicHTML" />

<block name='content'>
<link href="__COMMON__/daterangepicker/daterangepicker-bs3.css"
    rel="stylesheet" type="text/css" />
<script src="__COMMON__/daterangepicker/moment.min.js"
    type="text/javascript"></script> <script
    src="__COMMON__/daterangepicker/daterangepicker.js"
    type="text/javascript"></script>
<link rel="stylesheet" href="__COMMON__/bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet"
    href="__COMMON__/bootstrap/css/font-awesome.min.css" />

<div class="wrapper">
<section class="content ">
    <!-- Main content -->
    <div class="container-fluid">
        <div class="pull-right">
            <a href="javascript:history.go(-1)" data-toggle="tooltip" title=""
                class="btn btn-default" data-original-title="返回"><i
                class="fa fa-reply"></i></a> 
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">
                    <i class="fa fa-list"></i> 编辑优惠套餐
                </h3>
            </div>
            <div class="panel-body ">
                <!--表单数据-->
                <form method="post" id="promotion">
                    <!--通用信息-->
                    <div class="tab-content col-md-10">
                        <div class="tab-pane active" id="tab_tongyong">
                            <table class="table table-bordered">
                                <tbody>
                                    
                                    <tr>
                                        <td>商品列表：</td>
                                        <td>
                                        <a class="btn btn-primary" href="javascript:void(0);" onclick="sub_list(this)"><i class="fa fa-search"></i>选择商品</a>&nbsp;<small>最多5个商品</small>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2">
                                            <div class="form-group">
                                                <table class="table table-bordered">
                                                    <thead>
                                                        <tr>
                                                            <th style="display: none">选择</th>
                                                            <th class="text-left">商品名称</th>
                                                            <th class="text-left">优惠价格</th>
                                                            <th class="text-left">价格</th>
                                                            <th class="text-left">库存</th>
                                                            <th class="text-left">操作</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody id="sub_list">

                                                    <foreach name="list" item="vo">
                                                        <tr>
                                                            <td style="display: none">
                                                            <input type="hidden" name="goods_id" checked="checked" value="{$vo['goods_id']}" />
                                                            </td>
                                                            <td class="text-left">{$vo.title}</td>
                                                            <td class="text-left"><input type="text" class="discount" name="discount" value="{$vo.discount}"></td>
                                                            <td class="text-left">{$vo.price_member}</td>
                                                            <td class="text-left">{$vo.stock}</td>
                                                            <td class="text-left">
                                                    <a href="javascript:void(0)" onclick="delete_row(this)">删除</a>
                                                            </td>
                                                        </tr>
                                                    </foreach>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>价格：</td>
                                        <td><small id="discount">优惠价格:￥{$discount|default='0.00'}</small>&nbsp;<small id="total">总价:￥{$total|default='0.00'}</small>
                                        </td>
                                    </tr>
                                    <tr style="display: none;">
                                        <td><input id="combo_id" type="hidden" name="id" value="{$package_id|default=0}" />
                                        </td>
                                    </tr>
                                    
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td><input class="btn btn-default" type="reset"
                                            value="重置"/>
                                            <input type="hidden" name="" value="">
                                        </td>
                                        <td class="text-right">
                                        <input class="btn btn-primary" type="button" onclick="add(this)" value="保存"></td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </form>
                <!--表单数据-->
            </div>
        </div>
    </div>
</section>
</div>
<script type="text/javascript" src="__COMMON__/js/jquery-form.js"></script> 
<script type="text/javascript" src="__COMMON__/js/alert.js?a={:time()}"></script> 

<script type="text/javascript">

function callBack(tableHtml, elementID)
{
    layer.closeAll('iframe');
    $('#'+elementID).append(tableHtml);
    $('#'+elementID).find('tr').each(function(index, obj) {
        var len = $(obj).find('td .discount').length;
        if (len > 0) {
            return true;
        }
        var td = $('<td></td>');
        td.addClass('text-left');
        var input = $('<input type=text> </input>');
        input.attr('name', 'discount');
        input.addClass('discount');
        td.append(input);
        $(obj).find('td').eq(1).after(td);
    });

    sum_price();
}

// 删除行
function delete_row(obj) 
{
    $(obj).parent().parent().remove();
    sum_price();
}

// 计算总价
function sum_price()
{
    var total    = 0;
    var discount = 0;
    $('#sub_list tr').each(function(index, obj){
        var price = $(obj).find('td').eq(2).find('input').val();
        price     = parseFloat(price);
        discount += isNaN(price) ? 0 : price;

        price = $(obj).find('td').eq(3).text();
        price = parseFloat(price);
        total += isNaN(price) ? 0 : price;

    });
    $('#discount').text('优惠价格:￥'+discount.toFixed(2));
    $('#total').text('商品总价:￥'+total.toFixed(2));
}

// 套餐商品列表
function sub_list(obj)
{
    var id_list  = '';
    var obj_list = $('#sub_list tr');
    obj_list.each(function(index, obj) {
        var id   = $(obj).find('input').eq(0).val();
        id_list += ',' + id;
    });

    if (id_list.length > 0) {
        id_list = id_list.substring(1);
    }
    
    var url = '{:U('searchGoods', ['multi'=>1, 'eleID'=>'sub_list'])}';
    url += '/id_list/'+id_list;
    Tool.selectGoods(url);
}

function add(obj)
{
    var url = "{:U('Combo/packageHandle')}";

    var item_list = $('#sub_list tr');
    if (item_list.length > 5) {
        layer.msg('最多选择5件套餐商品');
        return false;
    }
    if (item_list.length < 1) {
        layer.msg('请添加套餐商品');
        return false;
    }

    var flag = 0;
    var sub_ids = new Array();
    item_list.each(function(index, obj) {
        var item_id  = $(obj).find('td input').eq(0).val();
        var item_val = $(obj).find('td input').eq(1).val();
        if (isNaN(parseFloat(item_val))) {
            layer.msg('请正确填写套餐商品的价格!!!');
            flag = 1;
            return false;
        }
        sub_ids[sub_ids.length] = item_id+':'+item_val;
    });
    if (flag) {
        return false;
    }

    var data = {
        'sub_ids':  sub_ids,
    };

    var id = $('#combo_id').val();
    if (id != 0) {
        data.id = id;
        url += '/act/save';
    } else {
        url += '/act/add';
    }

    StandardPost(url, data);
}

function StandardPost(url,args) 
{
    var form = $("<form method='post'></form>");
    form.attr({"action":url});
    for (arg in args)
    {
        var input = $("<input type='hidden'>");
        input.attr({"name":arg});
        input.val(args[arg]);
        form.append(input);
    }
    $(document.body).append(form);
    form.submit();
}

</script>
</block>